package ecom.stateless;

import java.util.List;

import ecom.api.IAdminRemote;
import ecom.entity.AdminEntity;
import ecom.entity.AnnonceChercheurEntity;
import ecom.entity.AnnonceProprietaireEntity;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.*;

@Stateless (mappedName = "ecom.stateless.AdminStateless@Remote", description = "Admin stateless", name = "AdminStateless")
public class AdminStateless implements IAdminRemote {

    Log log = LogFactory.getLog(ChercheurStateless.class);


    AnnonceChercheurStateless ACS = new AnnonceChercheurStateless();
    AnnonceProprietaireStateless APS = new AnnonceProprietaireStateless();
    OptionPayanteStateless OPS = new OptionPayanteStateless();
    @PersistenceContext
    private EntityManager em;

    public void create(AdminEntity adminEntity) {
        em.persist(adminEntity);
    }

    public void create(String nom, String prenom, String pseudo, String mdp,
            String ville, String role) {
        
        AdminEntity myAdmin = new AdminEntity();
        myAdmin.setNom(nom);
        myAdmin.setPrenom(prenom);
        myAdmin.setVille(ville);
        myAdmin.setPseudo(pseudo);
        myAdmin.setMdp(mdp);
        myAdmin.setVille(ville);
        myAdmin.setRole(role);
        em.persist(myAdmin);
        log.info("On vient de faire persister admin " + pseudo);
    }

    public void edit(AdminEntity adminEntity) {
        em.merge(adminEntity);
    }

    public void remove(AdminEntity adminEntity) {
        em.remove(em.merge(adminEntity));
    }

    public AdminEntity find(Object id) {
        return em.find(AdminEntity.class, id);
    }

    public List<AdminEntity> findAll() {
        return em.createQuery("select object(o) from AdminEntity as o").getResultList();
    }

//     @Override
//    public List<AnnonceChercheurEntity> printAnnoncesChercheurs() {
//
//        return ACS.FindAnnoncesChercheur();
//    }
//
//     //@Override
//    public List<AnnonceProprietaireEntity> printAnnoncesProprio() {
//
//        return APS.FindAnnoncesProprietaire();
//    }
//
//    public void modifyPriceOptionPayante(long id, float prix) {
//
//        OPS.getOptionPayanteByID(id).setPrix(prix);
//
//    }
//
//    public void modifyLifeCycleOptionPayante(long id, int duree) {
//
//        OPS.getOptionPayanteByID(id).setDureedevie(duree);
//    }
//
//    @Override
//    public void removeOptionPayante(long id) {
//
//        OPS.getOptionPayanteByID(id);
//
//    }
//
//    @Override
//    public void removeAnnonceChercheur(long id) {
//
//        ACS.DeleteAnnonceChercheur(id);
//    }
//
//    @Override
//    public void removeAnnonceProprio(long id) {
//
//        APS.DeleteAnnonceProprietaire(id);
//    }

    @Override
    public AdminEntity findByPseudo(String pseudo){
    	java.util.List <AdminEntity> list = em.createQuery("SELECT p FROM AdminEntity p WHERE p.myUtilisateur.myCompteEntity.pseudo='"+ pseudo +"'").getResultList();
    	if(list.size() == 1) {
            log.debug("AdminStateless : liste contenant : " + list.get(0).getPseudo());
    		return list.get(0);

    	} else {
                log.debug("AdminStateless : la liste est null");
    		return null;
        }
    }
}
